package springbook.user.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import springbook.user.domain.User;

public class UserDaoJdbc implements UserDao{

	private JdbcTemplate jdbcTemplate;
	
	private RowMapper<User> userMapper = new RowMapper<User>() {
		@Override
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setId(rs.getString("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));
			return user;
		}
	};

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public void add(final User user) {
		this.jdbcTemplate.update(
				"INSERT INTO users(id, name, password) VALUES(?, ?, ?)",
				user.getId(), user.getName(), user.getPassword());
	}
	
	@Override
	public User get(String id){
		return this.jdbcTemplate.queryForObject(
				"SELECT * FROM users WHERE id = ?", new Object[] { id },
				this.userMapper);

	}
	
	@Override
	public void deleteAll() {
		this.jdbcTemplate.update("DELETE FROM users");
	}
	
	@Override
	public int getCount() {
		return this.jdbcTemplate.queryForInt("SELECT COUNT(*) FROM users");

	}
	
	@Override
	public List<User> getAll() {
		return this.jdbcTemplate.query("SELECT * FROM users ORDER BY id ASC",
				this.userMapper);
	}

}
